package com.ideaaedi.zoo.example.springboot.websocket.util;

import com.ideaaedi.commonds.constants.SymbolConstant;

/**
 * websocket 相关id、key 处理工具类
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
 * src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 1.0.0
 */
public final class WebSocketUidUtil {
    
    /**
     * websocket连接唯一标识（占位符为：连接唯一标识）
     */
    public static final String WS_SESSION_UUID_REDIS_PREFIX = "ws:session:%s";
    
    /**
     * 构建sessionUuid对应的redis key
     *
     * @param sessionUuid websocket session唯一标识
     *
     * @return sessionUuid对应的redis key
     */
    public static String buildSessionUuidRedisKey(String sessionUuid) {
        return String.format(WS_SESSION_UUID_REDIS_PREFIX,  sessionUuid);
    }
    
    /**
     * 解析sessionUuid对应的redis key，获取sessionUuid
     *
     * @param sessionUuidRedisKey sessionUuid对应的redis key
     *
     * @return sessionUuid
     */
    public static String obtainSessionUuid(String sessionUuidRedisKey) {
        try {
            String[] arr = sessionUuidRedisKey.split(SymbolConstant.COLON);
            int length = arr.length;
            return arr[length - 1];
        } catch (Exception e) {
            throw new IllegalArgumentException("parse session-uuid-redis-key fail. curr sessionUuidRedisKey -> " + sessionUuidRedisKey, e);
        }
    }
}
